from pyflag.ColumnTypes import StringType, TimestampType, FilenameType, InodeIDType
import pyflag.Reports as Reports
import pyflag.FlagFramework as FlagFramework
import pyflag.DB as DB
from pyflag.Scanner import *
import pyflag.Scanner as Scanner
import FileFormats.MozHist as MozHist
import FileFormats.MozCache as MozCache
import pyflag.pyflaglog as pyflaglog
import pyflag.Registry as Registry
import re,cgi
from FileFormats.HTML import decode_entity, HTMLParser, url_unquote

import sqlite3

def is_moz3_history(filename):
    conn = sqlite3.connect(os.path.abspath(filename))
    c = conn.cursor()
    try:
        query = c.execute("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name IN ('moz_places')")
    except sqlite3.Error:
        return False
    row = query.fetchone()
    table_count = row[0]
    return table_count == 1


def iterate_moz_history(filename):
    conn = sqlite3.connect(os.path.abspath(filename))
    query = """SELECT url, visit_count, title, last_visit_date, rev_host
               FROM moz_places
               WHERE last_visit_date
            """
    cursor = conn.cursor().execute(query)
    row = cursor.fetchone()
    while row:
        yield row
        row = cursor.fetchone()


import pyflag.CacheManager as CacheManager

class Moz3HistScan(Scanner.GenScanFactory):
    """ Scan for Mozilla history files """
    default = True
    depends = ['TypeScan']
    group = 'FileScanners'
    
    class Scan(Scanner.StoreAndScanType):
        types = ('application/x-sqlite',)

        def external_process(self, fd):
            dbh = DB.DBO(self.case)
            filename = CacheManager.MANAGER.provide_cache_filename(self.case, fd.inode)
            if is_moz3_history(filename):
                #pyflaglog.log(pyflaglog.DEBUG, fd.stat()['name'])
                for url, visit_count, title, last_visit_date, rev_host in iterate_moz_history(filename):
                    result = dict(
                            inode_id = fd.lookup_id(),
                            url  = url,
                            _LastVisitDate = "from_unixtime('%s')" % str(last_visit_date)[:10],
                            VisitCount = visit_count,
                            name = title,
                            host = rev_host[::-1],
                    )
                    dbh.insert('mozilla_history', **result)


import pyflag.tests
import pyflag.pyflagsh as pyflagsh


class Moz3HistScanTest(pyflag.tests.ScannerTest):
    """ Test Firefox 3 History scanner """
    test_case = "PyFlagTestCase"

    subsystem = 'Mounted'
    fstype = 'Mounted'
    test_file = 'up'

    def test01RunScanner(self):
        """ Test history 3 scanner """
        env = pyflagsh.environment(case=self.test_case)
        pyflagsh.shell_execv(env=env, command="scan",
                             argv=["*",'Moz3HistScan'])
        dbh = DB.DBO(self.test_case)
        dbh.execute("select count(*) from mozilla_history")
        row = dbh.fetch()
        assert row['count(*)']
